package com.mark.controller.portal;

import com.google.common.base.Splitter;
import com.mark.common.ServerResponse;
import com.mark.controller.common.CurrentUser;
import com.mark.enums.CartEnum;
import com.mark.enums.UserStateEnum;
import com.mark.exception.CartException;
import com.mark.pojo.User;
import com.mark.service.ICartService;
import com.mark.vo.CartVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Mark on 2018/3/27.
 * 购物车控制器
 */

@Controller
@RequestMapping("/cart/")
public class CartController {
    @Autowired
    private ICartService iCartService;

    //1.查看购物
    @RequestMapping(value = "cart_list.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse CartList(HttpServletRequest httpServletRequest){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        return iCartService.getCartList(currentUser.getId());
    }

    //2.添加商品到购物车
    @RequestMapping(value = "add_list.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse add(HttpServletRequest httpServletRequest, Integer productId, Integer count){
        if(productId < 0 || count < 0){
            return ServerResponse.createByIllegalArgument("输入的商品id和数量的参数值不合法！");
        }
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        return iCartService.addProductToCart(currentUser.getId(), productId, count);
    }

    //3.更新购物车
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> update(HttpServletRequest httpServletRequest,Integer count,Integer productId){
        if(productId < 0 || count < 0){
            return ServerResponse.createByIllegalArgument("输入的商品id和数量的参数值不合法！");
        }
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        return iCartService.updateCart(currentUser.getId(),productId,count);
    }

    //4.删除购物车
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> deleteProduct(HttpServletRequest httpServletRequest,String productIds){
        List<String> productList = Splitter.on(",").splitToList(productIds);
        if (CollectionUtils.isEmpty(productList)) {
            return ServerResponse.createByIllegalArgument("输入的参数值不合法！");
        }
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        return iCartService.deleteProduct(currentUser.getId(),productList);
    }

    //5.全选
    @RequestMapping(value = "select_all",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> selectAll(HttpServletRequest httpServletRequest){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        return iCartService.selectOrUnSelect(currentUser.getId(),null, CartEnum.CHECKED.getState());
    }

    //6.全反选
    @RequestMapping(value = "un_select_all",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> unSelectAll(HttpServletRequest httpServletRequest){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        return iCartService.selectOrUnSelect(currentUser.getId(),null, CartEnum.UN_CHECKED.getState());
    }

    //7.单选
    @RequestMapping(value = "select.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> select(HttpServletRequest httpServletRequest,Integer productId ){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        return iCartService.selectOrUnSelect(currentUser.getId(), productId, CartEnum.CHECKED.getState());
    }

    //8.单反选
    @RequestMapping(value = "un_select.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> unSelect(HttpServletRequest httpServletRequest,Integer productId ){
        if(productId < 0){
            return ServerResponse.createByIllegalArgument("输入的参数值不合法");
        }
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        return iCartService.selectOrUnSelect(currentUser.getId(), productId, CartEnum.UN_CHECKED.getState());
    }

    //9，获取购物车商品的数量
    @RequestMapping(value = "get_cart_product_count.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<Integer> getCartProductCount(HttpServletRequest httpServletRequest){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        return iCartService.getCartProductCount(currentUser.getId());
    }
}
